﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using ACP.Business;
using ACP.DTO;
using System.Web.Security;

namespace ACP.Web.UI.admin
{
    public partial class addQuestion : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                System.Web.Security.MembershipUser user = Membership.GetUser();
                if (user != null)
                {
                    string[] adminusers = ConfigurationManager.AppSettings["adminusers"].ToLower().Split('|');
                    Session["userName"] = user.UserName.ToLower(); //TBD
                    bool booluserExist = adminusers.Contains(Convert.ToString(Session["userName"]));
                    if (booluserExist)
                    {
                        bindDropdowns();
                    }
                    else
                    {
                        Response.Redirect("~/Default.aspx", false);
                    }
                }
                else
                {
                    Response.Redirect("~/Default.aspx", false);
                }
            }
        }

        private void bindDropdowns()
        {
            try
            {
                ddlExampType.DataSource = ExamTypeLogic.GetExaminationsTypes();
                ddlExampType.DataTextField = "examname";
                ddlExampType.DataValueField = "examid";
                ddlExampType.DataBind();
            }
            catch (Exception ex)
            {
                acptest_ErrorLog errorLog = new acptest_ErrorLog()
                {
                    ErrorMessage = ex.Message,
                    ErrorFunction = "bindDropdowns",
                    InsertedTime = DateTime.Now,
                    ModuleName = "Questions.aspx"
                };
                ACPErrorLogLogic.ErrorLog(errorLog);
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                question newQuestion = new question()
                {
                    examid = Convert.ToInt32(ddlExampType.SelectedValue),
                    questiontype = ddlChoiceType.SelectedValue,
                    question1 = txtQuestion.Text,
                    option1 = txtOption1.Text,
                    option2 = txtOption2.Text,
                    option3 = txtOption3.Text,
                    option4 = txtOption4.Text,
                    description = txtDescription.Text,
                    answer = txtAnswer.Text,
                    uploadedby = "Kishore", //TBD
                    complexity = ddlComplexity.SelectedValue,
                    issample = ddlQuestionType.SelectedValue == "1" ? true : false,
                    referenceid =1, //TBD
                    status = true, 
                    uploadeddate = DateTime.Now
                };
                QuestionsLogic.InsertQuestion(newQuestion);
            }
            catch (Exception ex)
            {
                acptest_ErrorLog errorLog = new acptest_ErrorLog()
                {
                    ErrorMessage = ex.Message,
                    ErrorFunction = "bindDropdowns",
                    InsertedTime = DateTime.Now,
                    ModuleName = "Questions.aspx"
                };
                ACPErrorLogLogic.ErrorLog(errorLog);
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            ddlExampType.SelectedIndex = 0;
            ddlQuestionType.SelectedIndex = 0;
            txtQuestion.Text = string.Empty;
            txtOption1.Text = string.Empty;
            txtOption2.Text = string.Empty;
            txtOption3.Text = string.Empty;
            txtOption4.Text = string.Empty;
            txtDescription.Text = string.Empty;
            txtAnswer.Text = string.Empty;
            ddlComplexity.SelectedIndex = 0;
            ddlChoiceType.SelectedIndex = 0;
        }
    }
}